package ua.kpi.fpm.km61.strat.sp.la.lexem;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import ua.kpi.fpm.km61.strat.sp.la.MarkedString;
import ua.kpi.fpm.km61.strat.sp.la.ResString;

public abstract class Lexem {

    private List<ResString> result = new ArrayList<ResString>();
    int priority = 0;
    String pattern = "";
    String name = "";

    private static final String DELIM = "\\s+";

    public Lexem() {
    }

    Lexem(int priority, String name) {
	this.priority = priority;
	this.name = name;
    }

    public List<ResString> getResult() {
	return result;
    }

    public void setResult(List<ResString> result) {
	this.result = result;
    }

    public static List<MarkedString> buildLecsem(List<MarkedString> inpText) {
	List<MarkedString> lexemList = new ArrayList<MarkedString>();
	if (inpText == null) {
	    return lexemList;
	}
	for (int i = 0; i < inpText.size(); i++) {
	    List<MarkedString> stringLexemList = Lexem.scanText(inpText.get(i));
	    lexemList.addAll(stringLexemList);
	}
	return lexemList;
    }

    private static List<MarkedString> scanText(MarkedString curStr) {
	List<MarkedString> lexemList = new ArrayList<MarkedString>();
	if (curStr == null) {
	    return lexemList;
	}
	Scanner scan = new Scanner(curStr.getStr());
	scan.useDelimiter(DELIM);
	System.out.println(curStr.getStr());
	while (scan.hasNext()) {
	    String st = scan.next();
	    if (!st.equals("")) {
		MarkedString fw = new MarkedString();
		fw.setStrnum(curStr.getStrnum());
		fw.setStr(st);
		fw.setColnum(curStr.getStr().indexOf(st) + 1);
		lexemList.add(fw);
	    }
	}
	return lexemList;
    }

}
